(function (T) {
  var serverUrl = window.location.origin;

  let sdk_name = "anyPortal";

  let _anyPortal = new (function () {
    this.ready = function (param, func) {
      console.log('{}', param)
      window.onEmpReady = function () {
        console.log('--onEmpReady--', '初始化emp')

      }
      func()
      console.log('---r-')
    };

    //config
    this.config = function (data) {};

    //error
    this.error = function (data) {};

    /**
     * 验证钉钉，获取用户鉴权code
     * @param func
     */
    this.requestAuthCode = function (func) {}

    /**
     * 获取用户信息
     * @param param 钉钉提供的code
     * @param func 方法对象
     */
    this.getAccountInfo = function (param, func) {
      let ajaxUrl = `${serverUrl}/zm/zmglpt-integration-web/mobile/getAccountInfo.do?usertoken=${param.usertoken}&token=${param.token}&t=${new Date().getTime()}`
      console.log('ajaxUrl------getAccountInfo', ajaxUrl)
      post_methods(
          ajaxUrl,
          {},
          function (data) {
            func.onSuccess(data)
          },
          function (err) {
            func.onFail(err)
          }
      )
    }


    this.saveAppAccountRel = function (param, func) {}
  })

  function _setNameSpace(obj, rootName) {
    if (rootName && rootName !== "") {
      obj["_namespace"] = rootName;
    }
    for (const key in obj) {
      let type = typeof obj[key];
      if (type === "object") {
        //console.log("object:", key);
        obj[key]["_namespace"] = (obj["_namespace"] ? obj["_namespace"] : "") + "." + key;
        _setNameSpace(obj[key]);
      } else if (type === "function") {
        obj[key]["_fullName"] = obj["_namespace"] + "." + key;
        //console.log("function:", key);
        //console.log("_fullName:", obj["_namespace"] + "." + key);
      }
    }
  }
  _setNameSpace(_anyPortal, sdk_name);


  const _handler = {
    get(target, key) {
      console.log(target, key)
      if (typeof target[key] === "object") {
        return new Proxy(target[key], _handler);
      } else if (typeof target[key] === "function") {
        return new Proxy(target[key], {
          apply(t, ctx, args) {
            let fnFullName = target[key]["_fullName"];
            let result = Reflect.apply(...arguments);
            console.log("result::", result)
            return result;
          }
        });
      }
    }
  };

  // ajax-post方法
  const post_methods = function (url, data, successFun, errorFun) {
    $.ajax({
      type: "post",
      url: url,
      data: data,
      cache: false,
      dataType: "JSON",
      contentType: "application/json; charset=UTF-8",
      success: function (data) {
        successFun(data)
      },
      error: function (err) {
        errorFun(err)
      }
    })
  }

  T[sdk_name] = new Proxy(_anyPortal, _handler);
})(window);
